﻿using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace SmartAgricultureService.Models
{
    public class MqttData
    {
        public long Time { get; set; }
        public string DeviceId { get; private set; }
        public string AreaCode { get; private set; }
        public MqttDataDetail Data { get; private set; }
        public static MqttData Create(string areaId, MqttDataDetail data = null)
        {
            return new MqttData
            {
                Data = data,
                AreaCode = areaId,
                DeviceId = string.Empty,
                Time = DateTimeOffset.UtcNow.ToUnixTimeSeconds()
            };
        }
        public static MqttData Create(string areaCode, string deviceId, MqttDataDetail data = null)
        {
            return new MqttData
            {
                Data = data,
                DeviceId = deviceId,
                AreaCode = areaCode,
                Time = DateTimeOffset.UtcNow.ToUnixTimeSeconds()
            };
        }
        public override string ToString()
        {
            var jsonSetting = new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore,
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            };
            return JsonConvert.SerializeObject(this, Formatting.Indented, jsonSetting);
        }
    }
}